package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.beans.*;

public class CardModel extends ModelDAO
{
	Connection connection = null;
	ResultSet resultSet = null; 

	/*
	 * Add cards in the database
	 * @param card : the card to add
	 */
	public int addCard(Card card, Long userId, Long roundId)
	{		
		PreparedStatement prepStat = null;
		String query = null;
		int valErreur = 0;

		try
		{
			connection = super.getConnection();
			query = "INSERT INTO round_card (u_id, r_id, rca_color, " +
			"rca_value) VALUES(?,?,?,?)";
			prepStat = connection.prepareStatement(query);

			prepStat.setLong(1, userId);
			prepStat.setLong(2, roundId);
			prepStat.setInt(3, card.suitToInt());
			prepStat.setInt(4, card.rankToInt());			

			// Execute the request
			valErreur = prepStat.executeUpdate();
			
			resultSet = prepStat.getGeneratedKeys(); 
			if ( resultSet != null && resultSet.next() ) 
			{ 
				// Get the last ID insert
			    valErreur = resultSet.getInt(1); 
			}
		}
		catch(Exception ex)
		{
			valErreur = 0;
			System.out.println("Cards - Insertion impossible");
			ex.printStackTrace();
		}

		try{
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return valErreur;
	}
	
	/*
	 * Get the cards of a player who is playing now
	 * @param userId : the Id of the user we want to get his cards
	 */
	public ArrayList<Card> getPlayerCards(Long userId){
		PreparedStatement prepStat = null;
		ArrayList<Card> allCards = new ArrayList<Card>();
		String query = null;

		try{
			connection = super.getConnection();
			query = "SELECT * FROM user u" +
					" INNER JOIN round_on r ON r.u_id = u.u_id AND r.r_on = true" +
					" INNER JOIN round_card c ON c.r_id = r.r_id AND c.u_id = u.u_id" +
					" WHERE u.u_id=?";
			
			prepStat = connection.prepareStatement(query);
			prepStat.setLong(1, userId);
			resultSet=prepStat.executeQuery();

			if (resultSet!=null){
				while(resultSet.next()){
					// TODO : create the cards
					// allCards.add(card);
				}
			}

		} catch(Exception ex){
			System.out.println("Erreur de requete " + ex);
		}

		try{
			resultSet.close();
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return allCards;
	}
	
	/*
	 * Get the cards of a table
	 * @param roundId : the Id of the round 
	 */
	public ArrayList<Card> getTableCards(Long roundId){
		PreparedStatement prepStat = null;
		ArrayList<Card> allCards = new ArrayList<Card>();
		String query = null;

		try{
			connection = super.getConnection();
			query = "SELECT * FROM round r" +
					"LEFT JOIN round_card c ON c.r_id = r.r_id AND c.u_id = 0" +
					"WHERE r.r_id=?";
			
			prepStat = connection.prepareStatement(query);
			prepStat.setLong(1, roundId);
			resultSet=prepStat.executeQuery();

			if (resultSet!=null){
				while(resultSet.next()){
					// TODO : create the cards
					// allCards.add(card);
				}
			}

		} catch(Exception ex){
			System.out.println("Erreur de requete " + ex);
		}

		try{
			resultSet.close();
			prepStat.close();
			connection.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}

		return allCards;
	}
}